package craky.componentc;

import java.awt.Color;
import java.awt.Event;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollBar;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;

import com.abigdreamer.swing.component.UIResourceManager;

public class JCScrollTree extends JCScrollPane {
	private static final long serialVersionUID = -9002172235348323323L;
	private static final Color DISABLED_BG = UIResourceManager.getColor("TextDisabledBackground");
	private Border border;
	private Border disabledBorder;
	private Color background;
	private JCTree tree;

	public JCScrollTree(JCTree tree) {
		setViewportView(this.tree = tree);
		init();
	}

	public JCScrollTree() {
		this(new JCTree());
	}

	public JCScrollTree(TreeNode root) {
		this(new JCTree(root));
	}

	public JCScrollTree(TreeModel newModel) {
		this(new JCTree(newModel));
	}

	public JCScrollTree(TreeNode root, final JCPopupMenu mnuDisplay) {
		this(new JCTree(root));
		
		getTree().addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				if (event.getModifiers() == Event.META_MASK) {//getContentPane()
					JScrollBar vBar = JCScrollTree.this.getVerticalScrollBar();
					JScrollBar hBar = JCScrollTree.this.getHorizontalScrollBar();
					mnuDisplay.show(JCScrollTree.this, event.getX()-hBar.getValue(), event.getY()-vBar.getValue());
				}
			}
		});
	}

	private void init() {
		setBorder(new LineBorder(new Color(84, 165, 213)));
		setDisabledBorder(new LineBorder(new Color(84, 165, 213, 128)));
		setBackground(UIResourceManager.getWhiteColor());
		setHeaderVisible(false);
		this.tree.setBorder(new EmptyBorder(0, 7, 0, 0));
		this.tree.setDisabledBorder(this.tree.getBorder());
		this.tree.setVisibleInsets(0, 0, 0, 0);
		this.tree.setAlpha(0.0F);
	}

	public Border getDisabledBorder() {
		return this.disabledBorder;
	}

	public void setDisabledBorder(Border disabledBorder) {
		this.disabledBorder = disabledBorder;

		if (!isEnabled()) {
			super.setBorder(disabledBorder);
		}
	}

	public void setBorder(Border border) {
		this.border = border;
		super.setBorder(border);
	}

	public Color getDisabledForeground() {
		return this.tree.getDisabledForeground();
	}

	public void setDisabledForeground(Color disabledForeground) {
		this.tree.setDisabledForeground(disabledForeground);
	}

	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		super.setBorder(enabled ? this.border : this.disabledBorder);
		super.setBackground(enabled ? this.background : DISABLED_BG);
	}

	public void setBackground(Color background) {
		this.background = background;
		super.setBackground(background);
	}

	public JCTree getTree() {
		return this.tree;
	}
}